import { defineEventHandler, getQuery } from 'h3';

export default defineEventHandler(async (event) => {
  const query = getQuery(event);

  const categories = [
    {
      id: 'hotel',
      name: '酒店资源',
      description: '酒店、民宿、度假村等住宿资源',
      icon: 'hotel',
      color: '#1890ff',
      parentId: null,
      level: 1,
      sort: 1,
      status: 1,
      children: [
        {
          id: 'hotel_building',
          name: '酒店建筑',
          description: '酒店主楼、附楼等建筑资源',
          icon: 'building',
          color: '#1890ff',
          parentId: 'hotel',
          level: 2,
          sort: 1,
          status: 1,
          children: [],
        },
        {
          id: 'hotel_room',
          name: '客房资源',
          description: '标准间、套房、总统套房等',
          icon: 'room',
          color: '#1890ff',
          parentId: 'hotel',
          level: 2,
          sort: 2,
          status: 1,
          children: [],
        },
        {
          id: 'hotel_facility',
          name: '设施资源',
          description: '会议室、餐厅、健身房等',
          icon: 'facility',
          color: '#1890ff',
          parentId: 'hotel',
          level: 2,
          sort: 3,
          status: 1,
          children: [],
        },
      ],
    },
    {
      id: 'restaurant',
      name: '餐饮资源',
      description: '餐厅、厨房、餐饮设备等',
      icon: 'restaurant',
      color: '#52c41a',
      parentId: null,
      level: 1,
      sort: 2,
      status: 1,
      children: [
        {
          id: 'restaurant_space',
          name: '餐饮空间',
          description: '餐厅、包间、厨房等空间',
          icon: 'space',
          color: '#52c41a',
          parentId: 'restaurant',
          level: 2,
          sort: 1,
          status: 1,
          children: [],
        },
        {
          id: 'restaurant_equipment',
          name: '餐饮设备',
          description: '厨房设备、餐具、桌椅等',
          icon: 'equipment',
          color: '#52c41a',
          parentId: 'restaurant',
          level: 2,
          sort: 2,
          status: 1,
          children: [],
        },
      ],
    },
    {
      id: 'software',
      name: '软件服务',
      description: '管理系统、SaaS服务等',
      icon: 'software',
      color: '#722ed1',
      parentId: null,
      level: 1,
      sort: 3,
      status: 1,
      children: [
        {
          id: 'pms',
          name: 'PMS系统',
          description: '酒店管理系统',
          icon: 'pms',
          color: '#722ed1',
          parentId: 'software',
          level: 2,
          sort: 1,
          status: 1,
          children: [],
        },
        {
          id: 'pos',
          name: 'POS系统',
          description: '收银系统、点餐系统',
          icon: 'pos',
          color: '#722ed1',
          parentId: 'software',
          level: 2,
          sort: 2,
          status: 1,
          children: [],
        },
        {
          id: 'crm',
          name: 'CRM系统',
          description: '客户关系管理系统',
          icon: 'crm',
          color: '#722ed1',
          parentId: 'software',
          level: 2,
          sort: 3,
          status: 1,
          children: [],
        },
      ],
    },
    {
      id: 'service',
      name: '人力服务',
      description: '管理团队、培训服务、咨询服务等',
      icon: 'service',
      color: '#fa8c16',
      parentId: null,
      level: 1,
      sort: 4,
      status: 1,
      children: [
        {
          id: 'management',
          name: '管理服务',
          description: '酒店管理、餐厅管理等',
          icon: 'management',
          color: '#fa8c16',
          parentId: 'service',
          level: 2,
          sort: 1,
          status: 1,
          children: [],
        },
        {
          id: 'training',
          name: '培训服务',
          description: '员工培训、技能培训等',
          icon: 'training',
          color: '#fa8c16',
          parentId: 'service',
          level: 2,
          sort: 2,
          status: 1,
          children: [],
        },
        {
          id: 'consulting',
          name: '咨询服务',
          description: '业务咨询、技术咨询等',
          icon: 'consulting',
          color: '#fa8c16',
          parentId: 'service',
          level: 2,
          sort: 3,
          status: 1,
          children: [],
        },
      ],
    },
    {
      id: 'equipment',
      name: '设备资源',
      description: '机械设备、电子设备、家具等',
      icon: 'equipment',
      color: '#13c2c2',
      parentId: null,
      level: 1,
      sort: 5,
      status: 1,
      children: [
        {
          id: 'electronic',
          name: '电子设备',
          description: '电脑、投影仪、音响等',
          icon: 'electronic',
          color: '#13c2c2',
          parentId: 'equipment',
          level: 2,
          sort: 1,
          status: 1,
          children: [],
        },
        {
          id: 'furniture',
          name: '家具设备',
          description: '桌椅、床具、沙发等',
          icon: 'furniture',
          color: '#13c2c2',
          parentId: 'equipment',
          level: 2,
          sort: 2,
          status: 1,
          children: [],
        },
        {
          id: 'machinery',
          name: '机械设备',
          description: '清洁设备、维修工具等',
          icon: 'machinery',
          color: '#13c2c2',
          parentId: 'equipment',
          level: 2,
          sort: 3,
          status: 1,
          children: [],
        },
      ],
    },
    {
      id: 'spa',
      name: '休闲娱乐',
      description: 'SPA、娱乐设施、健身设备等',
      icon: 'spa',
      color: '#eb2f96',
      parentId: null,
      level: 1,
      sort: 6,
      status: 1,
      children: [
        {
          id: 'spa_room',
          name: 'SPA房间',
          description: '按摩房、汗蒸房、美容房等',
          icon: 'spa_room',
          color: '#eb2f96',
          parentId: 'spa',
          level: 2,
          sort: 1,
          status: 1,
          children: [],
        },
        {
          id: 'fitness',
          name: '健身设施',
          description: '健身房、游泳池、跑步机等',
          icon: 'fitness',
          color: '#eb2f96',
          parentId: 'spa',
          level: 2,
          sort: 2,
          status: 1,
          children: [],
        },
        {
          id: 'entertainment',
          name: '娱乐设施',
          description: 'KTV、棋牌室、游戏厅等',
          icon: 'entertainment',
          color: '#eb2f96',
          parentId: 'spa',
          level: 2,
          sort: 3,
          status: 1,
          children: [],
        },
      ],
    },
  ];

  // 过滤逻辑
  let filteredCategories = categories;

  if (query.parentId !== undefined) {
    if (query.parentId === null || query.parentId === 'null') {
      // 获取顶级分类
      filteredCategories = filteredCategories.filter(
        (category) => category.parentId === null,
      );
    } else {
      // 获取指定父级的子分类
      const parentCategory = categories.find((c) => c.id === query.parentId);
      filteredCategories = parentCategory ? parentCategory.children || [] : [];
    }
  }

  if (query.level) {
    const level = Number.parseInt(query.level as string);
    filteredCategories = filteredCategories.filter(
      (category) => category.level === level,
    );
  }

  if (query.status !== undefined) {
    const status = Number.parseInt(query.status as string);
    filteredCategories = filteredCategories.filter(
      (category) => category.status === status,
    );
  }

  if (query.keyword) {
    const keyword = query.keyword as string;
    filteredCategories = filteredCategories.filter(
      (category) =>
        category.name.includes(keyword) ||
        category.description.includes(keyword),
    );
  }

  // 排序
  filteredCategories.sort((a, b) => a.sort - b.sort);

  return {
    code: 0,
    data: filteredCategories,
    msg: '获取市场分类成功',
  };
});
